package org.dromara.agriculture.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.agriculture.domain.vo.AgriculturePlantMethodVo;
import org.dromara.agriculture.domain.bo.AgriculturePlantMethodBo;
import org.dromara.agriculture.service.IAgriculturePlantMethodService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 种植方法
 *
 * @author Robin
 * @date 2024-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/agriculture/plantMethod")
public class AgriculturePlantMethodController extends BaseController {

    private final IAgriculturePlantMethodService agriculturePlantMethodService;

    /**
     * 查询种植方法列表
     */
    @SaCheckPermission("agriculture:plantMethod:list")
    @GetMapping("/list")
    public TableDataInfo<AgriculturePlantMethodVo> list(AgriculturePlantMethodBo bo, PageQuery pageQuery) {
        return agriculturePlantMethodService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出种植方法列表
     */
    @SaCheckPermission("agriculture:plantMethod:export")
    @Log(title = "种植方法", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AgriculturePlantMethodBo bo, HttpServletResponse response) {
        List<AgriculturePlantMethodVo> list = agriculturePlantMethodService.queryList(bo);
        ExcelUtil.exportExcel(list, "种植方法", AgriculturePlantMethodVo.class, response);
    }

    /**
     * 获取种植方法详细信息
     *
     * @param methodId 主键
     */
    @SaCheckPermission("agriculture:plantMethod:query")
    @GetMapping("/{methodId}")
    public R<AgriculturePlantMethodVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long methodId) {
        return R.ok(agriculturePlantMethodService.queryById(methodId));
    }

    /**
     * 新增种植方法
     */
    @SaCheckPermission("agriculture:plantMethod:add")
    @Log(title = "种植方法", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AgriculturePlantMethodBo bo) {
        return toAjax(agriculturePlantMethodService.insertByBo(bo));
    }

    /**
     * 修改种植方法
     */
    @SaCheckPermission("agriculture:plantMethod:edit")
    @Log(title = "种植方法", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AgriculturePlantMethodBo bo) {
        return toAjax(agriculturePlantMethodService.updateByBo(bo));
    }

    /**
     * 删除种植方法
     *
     * @param methodIds 主键串
     */
    @SaCheckPermission("agriculture:plantMethod:remove")
    @Log(title = "种植方法", businessType = BusinessType.DELETE)
    @DeleteMapping("/{methodIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] methodIds) {
        return toAjax(agriculturePlantMethodService.deleteWithValidByIds(List.of(methodIds), true));
    }
}
